//
//  LWProjectListCtrl.m
//  laowu
//
//  Created by yinqy on 15/4/30.
//  Copyright (c) 2015年 长沙市岳麓区立早网络科技有限公司. All rights reserved.
//

#import "LWWorkReportListCtrl.h"
#import "WorkReportCell.h"
#import "LWProjReportBCtrl.h"
#import "LWProjReportMGCtrl.h"
#import "LWProjReportZCtrl.h"

@interface LWWorkReportListCtrl ()<UITableViewDataSource,UITableViewDelegate,WorkReportCellDelegate>
@property (nonatomic,strong)NSMutableArray *itemArray;
@end


@implementation LWWorkReportListCtrl

-(NSMutableArray *)itemArray{
    if (!_itemArray) {
        _itemArray = [NSMutableArray array];
    }
    return _itemArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTitle:@"工作汇报"];
    [self.enterBtn setBackgroundColor:THEME_COLOR];
    [self.enterBtn addTarget:self action:@selector(enterBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.tableView setDelegate:self];
    [self.tableView setDataSource:self];
    [self.tableView setTableFooterView:[UIView new]];
    
    [self.tableView addHeaderWithTarget:self action:@selector(headerRefresh)];
    [self.tableView addFooterWithTarget:self action:@selector(footerRefresh)];
    [self.tableView headerBeginRefreshing];
}

-(void)headerRefresh{
    [self requestDataWithMaxId:0];
}

-(void)footerRefresh{
    NSInteger maxId = 0;
    if (self.itemArray.count) {
        WorkReportItemResult *item = [self.itemArray lastObject];
        maxId = item.id-1;
    }
    [self requestDataWithMaxId:maxId];
}

-(void)requestDataWithMaxId:(NSInteger)maxId{
    [MeDataTool getWordReportListWithMaxId:maxId success:^(WorkReportListRes *result) {
        [Utils refreshComplete:self.tableView];
        if (result.isSucc) {
            //加载成功
            //判断maxid是否为0
            if (maxId==0) {
                //清空原有集合
                [self.itemArray removeAllObjects];
            }
            [self.itemArray addObjectsFromArray:result.datas];
            [self.tableView reloadData];
        }else{
            [DialogUtils showTipsWithView:nil String:result.msg];
        }
    } failure:^(NSError *error) {
        [Utils refreshComplete:self.tableView];
        [DialogUtils showTipsWithView:nil String:@"加载失败"];
    }];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    NSInteger group_id = [UserDefaultsHelper integerForKey:KEY_USER_GROUP_ID];
    if (!(group_id==1 ||group_id==2 || group_id == 4 || group_id==5)) {
        //隐藏底部按钮
        [self.enterBtn setHidden:YES];
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _itemArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    WorkReportCell *cell = [[WorkReportCell alloc] initWithTabelView:tableView indexPath:indexPath];
    [cell setItemInfo:_itemArray[indexPath.row]];
    [cell setDelegate:self];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 90;
}
-(void)workReportEditClick:(NSIndexPath *)indexPath{
    UIViewController *ctrl = nil;
    NSInteger group_id = [UserDefaultsHelper integerForKey:KEY_USER_GROUP_ID];
    if (group_id==1) {
        //民工汇报工作
        ctrl = [[LWProjReportMGCtrl alloc] initWithWorkReportItemResult:self.itemArray[indexPath.row]];
    }else if (group_id==2 || group_id == 4) {
        //班组长汇报工作
        ctrl = [[LWProjReportBCtrl alloc] initWithWorkReportItemResult:self.itemArray[indexPath.row]];
    }else if (group_id==5){
        //项目经理汇报工作
        ctrl = [[LWProjReportZCtrl alloc] initWithWorkReportItemResult:self.itemArray[indexPath.row]];
    }
    [self.navigationController pushViewController:ctrl animated:YES];
}


-(void)enterBtnClick:(UIButton *)btn{
    UIViewController *ctrl = nil;
    NSInteger group_id = [UserDefaultsHelper integerForKey:KEY_USER_GROUP_ID];
    if (group_id==1) {
        //民工汇报工作
        ctrl = [[LWProjReportMGCtrl alloc] init];
    }else if (group_id==2 || group_id == 4) {
        //班组长汇报工作
        ctrl = [[LWProjReportBCtrl alloc] init];
    }else if (group_id==5){
        //项目经理汇报工作
        ctrl = [[LWProjReportZCtrl alloc] init];
    }
    [self.navigationController pushViewController:ctrl animated:YES];
}


@end
